package demo.android.myapplication.demos.views

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import cn.android.basic.centerX
import cn.android.basic.centerY
import cn.android.basic.color
import cn.android.basic.dp

/**
 * <br>createBy Gw
 * <br>createTime: 2021/7/8 11:24
 */
class SportView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val radius = 100.dp
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val rectF = RectF()
    private val rect = Rect()
    private var text = "abag"
    private val fm = Paint.FontMetrics()

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawRing(canvas)
        drawText(canvas)
    }

    private fun drawText(canvas: Canvas) {
        paint.textSize = 50.dp
        paint.getFontMetrics(fm)
        paint.getTextBounds(text, 0, text.length, rect)
        canvas.drawText(text, centerX - rect.width() / 2,  centerY - (fm.bottom + fm.top) / 2, paint)
    }

    private fun drawRing(canvas: Canvas) {
        paint.color = "#90A4AF".color
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 10.dp
        paint.strokeCap = Paint.Cap.ROUND

        canvas.drawCircle(centerX, centerY, radius, paint)

        paint.color = "#FF387F".color
        rectF.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius)

        canvas.drawArc(rectF,270f, 240f, false, paint)

        paint.style = Paint.Style.FILL
    }
}